package com.example.autopia.view.Activities

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Toast
import com.example.autopia.R
import com.example.autopia.databinding.ActivityChannelsBinding
import com.example.autopia.model.Item.News
import com.example.autopia.model.Item.User
import com.example.autopia.model.Repositories.TestRepository
import com.example.autopia.model.Repositories.UsersRepository
import com.example.autopia.view.Adapters.ChannelsAdapter
import com.example.autopia.view.Adapters.NewsAdapter
import com.example.autopia.view.Listeners.ChannelsListener

class ChannelsActivity : AppCompatActivity() {
    private lateinit var binding: ActivityChannelsBinding
    private val userRepository = UsersRepository()
    private val adapter = ChannelsAdapter()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityChannelsBinding.inflate(layoutInflater)
        setContentView(binding.root)

        binding.btBack.setOnClickListener {
            finish()
        }

        binding.btAll.setOnClickListener {
            binding.btAll.setBackgroundResource(R.drawable.button_active_menu)
            binding.btVerifacated.setBackgroundResource(android.R.color.transparent)
            binding.loading.visibility = View.VISIBLE
            setAllData()
        }

        binding.btVerifacated.setOnClickListener {
            binding.btAll.setBackgroundResource(android.R.color.transparent)
            binding.btVerifacated.setBackgroundResource(R.drawable.button_active_menu)
            binding.loading.visibility = View.VISIBLE
            setOffData()
        }

        binding.recyclerView.adapter = adapter
        binding.recyclerView.visibility = View.GONE
        val test = ArrayList<User>()
        test.add(TestRepository.getUser())
        test.add(TestRepository.getUser())
        adapter.setData(test)

        setAllData()

        adapter.attachListener(object : ChannelsListener{
            override fun onChannelClick(userid : String) {
                val intent = Intent(this@ChannelsActivity,MyNewsActivity::class.java)
                intent.putExtra("id",userid)
                startActivity(intent)
            }
        })
    }
    private fun setAllData(){
        val items = userRepository.getAllItems()
        items.observe(this){
            adapter.setData(it)
            binding.recyclerView.visibility = View.VISIBLE
            binding.loading.visibility = View.GONE
        }
    }
    private fun setOffData(){
        val items = userRepository.getAllItems()
        items.observe(this){
            var list = it
            list = list.filter { it.verificated==1 } as ArrayList<User>
            adapter.setData(list)
            binding.recyclerView.visibility = View.VISIBLE
            binding.loading.visibility = View.GONE
        }
    }
}